import Quill from 'quill'
import store from '@/store/index'
const Embed = Quill.import('blots/embed')

class ParamTag extends Embed {
  static create (value) {
    let node = super.create()
    if (typeof value === 'object') {
      ParamTag.buildSpan(value, node)
    } else if (typeof value === 'string') {
      const valueObj = store.state.emailTemplate.allParams.find(item => item.id === value)
      if (valueObj) {
        ParamTag.buildSpan(valueObj, node)
      }
    }
    return node
  }
  static value (node) {
    return node.dataset.id
  }

  static buildSpan (value, node) {
    node.setAttribute('data-id', value.id)
    let paramSpan = document.createElement('span')
    paramSpan.classList.add('tag-inner')
    paramSpan.innerText = value.name
    node.appendChild(paramSpan)
  }
}

ParamTag.blotName = 'param-tag'
ParamTag.className = 'param-tag'
ParamTag.tagName = 'span'

export default ParamTag
